package com.sunagy.demand.front.controller.api;

import com.sunagy.demand.front.VO.ResultVO;
import com.sunagy.demand.front.exception.BuzException;
import com.sunagy.demand.front.exception.PrivilegeException;
import com.sunagy.demand.front.utils.ResultVOUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/27
 * time：下午2:37
 */
public class BaseApi {
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResultVO accessDeniedExceptionHandler(Exception e) {
        return ResultVOUtil.fail("未授权");
    }

    @ExceptionHandler(BuzException.class)
    @ResponseStatus(HttpStatus.OK)
    ResultVO buzExceptionHandler(BuzException e) {
        if(e.getReason() != null){
            return ResultVOUtil.fail(e.getCode(), e.getReason());
        }else{
            return ResultVOUtil.fail(e.getCode(), "系统异常");
        }
    }

    @ExceptionHandler(PrivilegeException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    ResultVO privilegeExceptionHandler(PrivilegeException e) {
        return ResultVOUtil.fail(e.getCode(), "权限不足");
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Object errorHandler(Exception e) {
        return ResultVOUtil.fail("系统异常");
    }

}
